﻿// Inner Fire 游戏引擎库
// AttackInfo - 游戏攻击配置
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 所属层级：应用层（4）
// 用于游戏中的攻击配置
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-07-05
#include "attack_info.h"
#include <game/resource/assets.h>

namespace ifire::game {

HitPhase::HitPhase(const ikit::XmlNode& node)
    : percent(node("Percent")), range(node("Range")) {
  percent *= 0.01f;
  range.x *= 0.01f;
  range.y *= 0.01f;
}

AttackInfo::AttackInfo(ConfigManager* manager, const ikit::XmlNode& node)
    : ConfigInfo(manager, node("Name"))
    , animation(node("Animation"))
    , duration(node("Duration"))
    , cancel_time(node("CancelTime")) {
  node.ReadItems("HitPhase", hit_phases);
}

void AttackInfo::Setup() { Assets::Setup(animation); }

float AttackInfo::GetDuration() {
  // 如果设置为0，表示必须从动画载入，使动画Ready
  if (duration < FLT_EPSILON) {
    animation->Ready();
    duration = animation->GetDuration();
    ikit::log("%s:%s duration is: %.5g",
        animation->GetAnimation()->name.c_str(), animation->name.c_str(),
        duration);
  }
  return duration;
}

float AttackInfo::GetCancelTime() const {
  constexpr float CANCEL_TIME_PERCENT = 0.70f;
  if (cancel_time < FLT_EPSILON){
    return duration * CANCEL_TIME_PERCENT;
  }
  return cancel_time;
}



} // namespace ifire::game